package jgl.vajra.kingkong.part.vehicle.activity;

import androidx.recyclerview.widget.LinearLayoutManager;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import jgl.vajra.kingkong.R;
import jgl.vajra.kingkong.bean.CommonAddressBean;
import jgl.vajra.kingkong.bean.WorkplaceBean;
import jgl.vajra.kingkong.databinding.ActivityCommonAddressBinding;
import jgl.vajra.kingkong.event.WorkplaceEvent;
import jgl.vajra.kingkong.manager.AccountManager;
import jgl.vajra.kingkong.manager.IntentManager;
import jgl.vajra.kingkong.part.vehicle.contract.CommonAddressContract;
import jgl.vajra.kingkong.part.vehicle.viewModel.CommonAddressViewModel;
import jgl.vajra.kingkong.widget.databindingadapter.BaseBindingItemPresenter;
import jgl.vajra.kingkong.widget.databindingadapter.SingleTypeBindingAdapter;
import jgl.vajra.kingkong.widget.mvvm.factory.CreateViewModel;
import jgl.vajra.kingkong.widget.mvvm.view.BaseMVVMActivity;
import jgl.vajra.kingkong.widget.retrofithelper.utils.AEACCoder;
import jgl.vajra.kingkong.widget.utils.ToastUtils;
import jgl.vajra.kingkong.widget.utils.UIUtils;

@CreateViewModel(CommonAddressViewModel.class)
public class CommonAddressActivity extends BaseMVVMActivity<CommonAddressViewModel, ActivityCommonAddressBinding> implements CommonAddressContract.View, BaseBindingItemPresenter {

    private String authorization;
    private SingleTypeBindingAdapter<CommonAddressBean> adapter;
    private List<CommonAddressBean> commonAddressBean;
    private CommonAddressBean itemData;
    private int type;

    @Override
    public int getLayoutId() {
        return R.layout.activity_common_address;
    }

    @Override
    public void initView() {
        super.initView();
        type = getIntent().getIntExtra("type", -1);
        authorization = AccountManager.getInstance().getLoginData(UIUtils.getContext()).authorization;
        mBinding.setView(this);

        //设置布局管理器
        mBinding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new SingleTypeBindingAdapter<>(this, null, R.layout.item_common_address);
        mBinding.recyclerView.setAdapter(adapter);
        adapter.setItemPresenter(this);

        requestNetData();
    }

    private void requestNetData() {
        mViewModel.getAddressList(authorization);
    }

    //添加常用地址
    @Override
    public void onAddCommonAddress() {
        CommonAddressBean itemData = new CommonAddressBean();
        IntentManager.toEditAddressActivity(this, itemData);
    }

    //编辑地址
    @Override
    public void onEditAddress(CommonAddressBean itemData) {
        IntentManager.toEditAddressActivity(this, itemData);
    }

    //删除地址
    @Override
    public void onDeleteAddress(CommonAddressBean itemData) {
        this.itemData = itemData;
        mViewModel.deleteAddress(authorization, itemData.id);
    }

    //选中地址
    @Override
    public void onSelectAddress(CommonAddressBean itemData) {
        WorkplaceBean bean = new WorkplaceBean();
        bean.name = itemData.contactName;
        bean.phone = AEACCoder.decrypt(itemData.contactPhone);
        bean.address = itemData.houseNumber;
        bean.city = itemData.city;
        bean.detailedAddress = itemData.address;
        bean.latitude = Double.parseDouble(itemData.latitude);
        bean.longitude = Double.parseDouble(itemData.longitude);
        EventBus.getDefault().post(new WorkplaceEvent(bean,type));
        finish();
    }

    @Override
    public void returnAddressListData(List<CommonAddressBean> data) {
        commonAddressBean = data;
        adapter.refresh(data);
    }

    @Override
    public void returnDeleteAddress() {
        ToastUtils.showShort("删除成功");
        commonAddressBean.remove(itemData);
        adapter.refresh(commonAddressBean);
    }

    @Override
    protected void onResume() {
        super.onResume();
        requestNetData();
    }

    @Override
    public void onItemClick(int position, Object itemData) {

    }
}
